iT邦幫忙

DAY 16
1

Java重點學習筆記系列 第 16

Java重點筆記十四:Java的日曆類別GregorianCalendar Class

  • 分享至 

  • xImage
  •  

Java提供一個名為GregorianCalendar的類別來實現,它是繼成自Calendar類別。

以下是GregorianCalendar的建構函數式:

GregorianCalendar() 

GregorianCalendar(int year, int month, int date) 

GregorianCalendar(int year, int month, int date, int hour, int minute) 

GregorianCalendar(int year, int month, int date, int hour, int minute, int second) 

從參數不難看出,它會根據提供的時間來建立日曆。當參數為空時,則取當下時間。

除了時間之外還可用時區與地區來建立日曆:

GregorianCalendar(Locale aLocale) 

GregorianCalendar(TimeZone zone)

GregorianCalendar(TimeZone zone, Locale aLocale) 

以下是GregorianCalendar提供的方法(method):

SN	Methods with Description
1	void add(int field, int amount) 
Adds the specified (signed) amount of time to the given time field, based on the calendar's rules.
2	protected void computeFields() 
Converts UTC as milliseconds to time field values.
3	protected void computeTime() 
Overrides Calendar Converts time field values to UTC as milliseconds.
4	boolean equals(Object obj) 
Compares this GregorianCalendar to an object reference.
5	int get(int field) 
Gets the value for a given time field.
6	int getActualMaximum(int field) 
Return the maximum value that this field could have, given the current date.
7	int getActualMinimum(int field) 
Return the minimum value that this field could have, given the current date.
8	int getGreatestMinimum(int field) 
Returns highest minimum value for the given field if varies.
9	Date getGregorianChange() 
Gets the Gregorian Calendar change date.
10	int getLeastMaximum(int field) 
Returns lowest maximum value for the given field if varies.
11	int getMaximum(int field) 
Returns maximum value for the given field.
12	Date getTime()
Gets this Calendar's current time.
13	long getTimeInMillis() 
Gets this Calendar's current time as a long.
14	TimeZone getTimeZone() 
Gets the time zone.
15	int getMinimum(int field) 
Returns minimum value for the given field.
16	int hashCode() 
Override hashCode.
17	boolean isLeapYear(int year)
Determines if the given year is a leap year.
18	void roll(int field, boolean up) 
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
19	void set(int field, int value) 
Sets the time field with the given value.
20	void set(int year, int month, int date) 
Sets the values for the fields year, month, and date.
21	void set(int year, int month, int date, int hour, int minute) 
Sets the values for the fields year, month, date, hour, and minute.
22	void set(int year, int month, int date, int hour, int minute, int second) 
Sets the values for the fields year, month, date, hour, minute, and second.
23	void setGregorianChange(Date date) 
Sets the GregorianCalendar change date.
24	void setTime(Date date) 
Sets this Calendar's current time with the given Date.
25	void setTimeInMillis(long millis) 
Sets this Calendar's current time from the given long value.
26	void setTimeZone(TimeZone value) 
Sets the time zone with the given time zone value.
27	String toString() 
Return a string representation of this calendar.

請看以下的例子:

import java.util.*;
  
public class GregorianCalendarDemo {

   public static void main(String args[]) {
      String months[] = {
      "Jan", "Feb", "Mar", "Apr",
      "May", "Jun", "Jul", "Aug",
      "Sep", "Oct", "Nov", "Dec"};
      
      int year;
      // Create a Gregorian calendar initialized
      // with the current date and time in the
      // default locale and timezone.
      GregorianCalendar gcalendar = new GregorianCalendar();
      // Display current time and date information.
      System.out.print("Date: ");
      System.out.print(months[gcalendar.get(Calendar.MONTH)]);
      System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
      System.out.println(year = gcalendar.get(Calendar.YEAR));
      System.out.print("Time: ");
      System.out.print(gcalendar.get(Calendar.HOUR) + ":");
      System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
      System.out.println(gcalendar.get(Calendar.SECOND));
      
      // Test if the current year is a leap year
      if(gcalendar.isLeapYear(year)) {
         System.out.println("The current year is a leap year");
      }
      else {
         System.out.println("The current year is not a leap year");
      }
   }
}

輸出如下:

Date: Oct 15 2014
Time: 11:25:27
The current year is not a leap year

上一篇
Java重點筆記十三:Java的Sleep功能
下一篇
Java重點筆記十五:Java的文件操作:FileInputStream & FileOutputStream
系列文
Java重點學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言